<%-- 
    Document   : change_kategory
    Created on : 14.04.2013, 3:03:31
    Author     : Yasya
--%>

<%@page import="javax.naming.NamingException"%>
<%@page import="delta.productcatalog.controller.CategoryFacadeRemote"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="delta.productcatalog.model.Category"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%!    private CategoryFacadeRemote categoryFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            categoryFacade = (CategoryFacadeRemote) ic.lookup(
                    "java:global/ProductCatalog/ProductCatalog-ejb/CategoryFacade!delta.productcatalog.controller.CategoryFacadeRemote");
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>

<script>
    function editCategory(){
        
        var checkboxes = document.getElementsByName("id_category");
        var checkbox;
        for(var i=0;i<checkboxes.length;i++){
            checkbox=checkboxes[i];
            if(checkbox.checked==true) {
                document.getElementById(checkbox.value).disabled=false;
                document.getElementById("ok_btn").style.display='block';
                document.getElementById("cancel_btn").style.display='block';
            }
        }
    }
    
    
</script>
<script>
    function submitForm(){
        if( document.getElementById("categ").value.length>0&&document.getElementById("categ").value.length<51){
            document.getElementById("addcategory_form").submit();
        }else{
            alert("Please enter correct data");
        }
    }
   
    function submitEditForm(){
        var checkboxes = document.getElementsByName("id_category");
        var checkbox;
        var input_id;
        for(var i=0;i<checkboxes.length;i++){
            checkbox=checkboxes[i];
            if(checkbox.checked==true) {
                input_id=checkbox.value;
            }
        }
        
        if( document.getElementById(input_id).value.length>0&&document.getElementById(input_id).value.length<51){
            document.getElementById("category_form").submit();
        }else{
            alert("Please enter correct data");
        }
    }
         
    
    
</script>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styl.css">

        <title>Work with product</title>
    </head>
    <body>

        <header>
            <div id ="top_name"> <h1> Warehouse employee</h1> </div>
            <div id="user-panel">
                <p> <a href="#">User name</a>
                    <a href="#">LogOut</a> </p>
            </div>
            <div id="menu">
                <div id="line"> <a href="#"> Orders</a> </div>
                <div id="line"> <a href="#"> Warehouses</a> </div>
                <div id="line"> <a href="#"> Product</a> </div>

                <div id="role"> <select name="role" size = 1> 
                        <option value=”1” >Customer</option>
                        <option value=”2”>Warehouse Employee</option> 
                        <option value=”3”>Courier</option> 
                        <option value=”4”, selected>Admin</option> 
                    </select> </div>
            </div>
        </header>

        <div id="main">
            <div id="find">  
                <form method="POST" action="find"> 
                    <p>Find:
                        <input type="text" name="Find" size="30" value="keyword" id="label"/>
                        <button id="go" type="submit" name="go" >Go</button> </p>
                </form>
            </div>

            <div id="export"> <button type="submit" name="export" >Export data</button>
            </div>

            <div id="sorted">
                <p> Category: <select  name="category" size = 1 disabled="disabled"> 

                    </select> 
                    <a href="change_category.jsp"> Create/delete category </a> </div> </p>

        <div id="change_category">
            <div id="tabl_cat">
                <form id="category_form" ction="EditCategory">
                    <table>
                        <TR>
                            <td></td>
                            <td id="head_table_first"><strong>Name</strong></td>
                        </TR>

                        <%for (Category c : categoryFacade.findAll()) {%>
                        <tr>

                            <td> <input type="checkbox" name="id_category" value=<%=c.getId_category()%>></td>
                            <td id="colomn_first">
                                <input id="<%=c.getId_category()%>" type="text" name="<%=c.getId_category()%>" value="<%=c.getName()%> " disabled>
                            </td>
                        </tr>
                        <%}%>



                    </table>
                </form>
            </div>

            <div id="new_categ">
                <p>New category:
                <form id="addcategory_form" action="AddCategory"> 
                    <input 
                        id="categ"
                        type="text" 
                        name="categoryName" 
                        size="20" />
                </form> 
                </p>

            </div>
            <div id="button_category">
                <button 
                    id="add" 
                    name="Add"                    
                    onclick="submitForm()">
                    Add                    
                </button> <br>

                <button 
                    id="delete" 
                    style="display: none"
                    type="submit" 
                    name="Delete" 
                    form="category_form"  
                    formaction="DeleteCategory">
                    Delete
                </button>

                <button 
                    id="edit" 
                    name="Edit" 
                    onclick="editCategory()">
                    Edit
                </button><br> 

                <button 
                    id="ok_btn" 
                    onclick="submitEditForm()"
                    name="Ok" 
                    style="display:none;" >
                    OK
                </button>

                <button 
                    id="cancel_btn" 
                    onclick="window.location.href='change_category.jsp'" 
                    name="cancel_btn" 
                    style="display:none;">
                    Cancel
                </button><br> 
            </div>


            </body>
            </html>